"""
Case Type   : ustore_DQL_MultiJoin
Case Name   : 创建 location_multi 表并插入数据
Create At   : 2022-04-22
Owner       : lonely-dance
Description :
    1、创建表
    2、使用copy命令插入数据
    3、环境清理
Expect      :
    1、创建成功
    2、数据插入成功
    3、清理成功
History     :
"""

import os
import subprocess
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class MultiJoin(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----- {os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_root = Node('PrimaryRoot')
        self.pri_user = Node('PrimaryDbUser')
        self.com = Common()
        self.constant = Constant()
        self.t_name = 'location_multi'
        self.copy_file = 'location_multi.txt'
        self.cur_path = os.path.join(macro.SCRIPTS_PATH, self.copy_file)
        self.parent_path = os.path.dirname(macro.DB_INSTANCE_PATH)
        self.target_path = os.path.join(self.parent_path, self.copy_file)

    def test_join(self):
        step_txt = '-----step1:创建表;expect: 创建成功-----'
        self.log.info(step_txt)
        create_sql = f"""drop table if exists {self.t_name} cascade ;
                         create table {self.t_name}
                         (
                         location_id integer not null ,
                         location_name char(40) ,
                         location_open_dt date ,
                         location_close_dt date ,
                         location_effective_dt date ,
                         location_total_area_meas number(18,4) ,
                         chain_cd varchar(50) ,
                         channel_cd varchar(50) ,
                         district_cd varchar(50) ,
                         parent_location_id integer ,
                         location_mgr_associate_id integer ,
                         location_type_cd varchar(50) ,
                         location_point varchar(100) null ,
                         customer_areas varchar(100) null
                         )
                         with (storage_type=ustore) partition by range (location_id )
                         (
                         partition location_1 values less than (0),
                         partition location_2 values less than (2500),
                         partition location_3 values less than (5000),
                         partition location_4 values less than (7500),
                         partition location_5 values less than (10000),
                         partition location_6 values less than (12500),
                         partition location_7 values less than (15000),
                         partition location_8 values less than (17500),
                         partition location_9 values less than (20000),
                         partition location_10 values less than (22500),
                         partition location_11 values less than (25000),
                         partition location_12 values less than (27500),
                         partition location_13 values less than (30000),
                         partition location_14 values less than (32500),
                         partition location_15 values less than (35000),
                         partition location_16 values less than (37500),
                         partition location_17 values less than (40000),
                         partition location_18 values less than (42500),
                         partition location_19 values less than (45000),
                         partition location_20 values less than (47500),
                         partition location_21 values less than (50000),
                         partition location_22 values less than (maxvalue)
                         );"""
        self.log.info(create_sql)
        create_result = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(create_result)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, create_result,
                      '执行失败:' + step_txt)

        step_txt = '-----step2:使用copy命令插入数据，expect: 数据插入成功-----'
        self.log.info(step_txt)
        text = '-----step2.1: 获取当前主机的IP地址;expect: 成功-----'
        self.log.info(text)
        cmd = "ifconfig -a|grep inet6 -a2|grep broadcast|awk '{print $2}'"
        self.log.info(cmd)
        self.cur_ip = subprocess.getoutput(cmd).strip()
        self.log.info(self.cur_ip)

        text = '-----step2.2: 复制文件到数据库主机对应目录 expect: 成功-----'
        self.log.info(text)
        if self.pri_user.db_host in self.cur_ip:
            self.log.info('当前主机是数据库主机，无需远程复制')
            cmd = f"cp {self.cur_path} {self.parent_path};"
            self.log.info(cmd)
            res = self.pri_sh.node.sh(cmd).result()
            self.log.info(res)
        else:
            self.com.scp_file(self.pri_user, self.copy_file,
                              self.parent_path)

        text = '-----step2.3: 查看数据库主机目标路径下文件 expect: 成功-----'
        self.log.info(text)
        cmd = f"ls -l {self.parent_path}| grep -E '{self.copy_file}'"
        self.log.info(cmd)
        res = self.pri_root.sh(cmd).result()
        self.log.info(res)
        self.assertNotIn('No such file or directory', res, '文件拷贝失败')
        self.assertIn(f'{self.copy_file}', res, '文件拷贝失败')

        text = '-----step2.4:使用copy命令插入数据，expect: 数据插入成功-----'
        self.log.info(text)
        self.log.info('---------------使用copy命令插入数据------------------')
        copy_sql = f'''source {macro.DB_ENV_PATH};
                        gsql -d {self.pri_user.db_name} -p {self.pri_user.db_port} \
                        -U {self.pri_user.db_user} -W {self.pri_user.db_password} \
                        -c "\\copy {self.t_name} from '{self.target_path}';
                        select count(*) from {self.t_name};";'''
        self.log.info(copy_sql)
        copy_result = self.pri_user.sh(copy_sql).result()
        self.log.info(copy_result)
        self.assertIn('50', copy_result, '执行失败:' + step_txt)

    def tearDown(self):
        self.log.info('-----this is teardown-----')
        step_txt = '-----step3: 环境清理，expect:清理成功-----'
        self.log.info(step_txt)
        self.log.info('由于后续用例依赖表，故先不删除')
        rm_cmd = f"rm -rf {self.target_path} "
        self.log.info(rm_cmd)
        rm_res = self.pri_root.sh(rm_cmd).result()
        self.log.info(rm_res)
        self.assertEqual(rm_res, '', '文件删除失败' + step_txt)
        self.log.info(f'----- {os.path.basename(__file__)} end-----')
